package com.example.demo.config;


import com.example.demo.utils.RequestData;
import com.example.demo.utils.ResponseData;
import net.sf.json.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.MediaType;
import org.springframework.http.converter.AbstractGenericHttpMessageConverter;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;


import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by reasahi on 2017/6/16.
 */
public class DataMessageConvert extends AbstractGenericHttpMessageConverter<Object> {
    @Override
    public boolean canRead(Type type, Class<?> contextClass, MediaType mediaType) {
        return ((Class) type).isAssignableFrom(RequestData.class);
    }

    @Override
    public boolean canWrite(Type type, Class<?> clazz, MediaType mediaType) {
        return ((Class) type).isAssignableFrom(ResponseData.class);
    }

    public List<MediaType> getSupportedMediaTypes() {
        return Collections.singletonList(MediaType.ALL);
    }

    protected boolean supports(Class<?> clazz) {
        return clazz.isAssignableFrom(Map.class);
    }

    public Object readInternal(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        return readMap(inputMessage);
    }

    private Object readMap(HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        Charset cs = Charset.forName("UTF-8");
        StringBuilder stringBuilder = new StringBuilder();
        InputStream inputStream = inputMessage.getBody();
        byte[] b = new byte[1024];
        int length;
        while ((length = inputStream.read(b)) != -1) {
            ByteBuffer byteBuffer = ByteBuffer.allocate(length);
            byteBuffer.put(b, 0, length);
            byteBuffer.flip();
            stringBuilder.append(cs.decode(byteBuffer).array());
        }
        String[] list = stringBuilder.toString().split(";");
        JSONObject paramJson = JSONObject.fromObject(list[0]);
        return new RequestData(paramJson);
    }

    public void writeInternal(Object o, Type type, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException {
        StringBuilder stringBuilder = new StringBuilder();
        String s = ((ResponseData) o).getValue();
        stringBuilder.append(s);

        outputMessage.getBody().write(stringBuilder.toString().getBytes());
    }

    public Object read(Type type, Class<?> contextClass, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException {
        return readMap(inputMessage);
    }
}